<?php

namespace App\Services;

use App\Models\OrderLease;

class UnicomService
{   
    //联通接口
    const UNICOM_ONE = [
       'goodsId'=>'512208096403',
       'productId'=>'91208411',
       'contractId'=>'91231333',
    ];//广东手机直播套餐199元-7折24个月合约
    const UNICOM_TWO = [
       'goodsId'=>'512208096405',
       'productId'=>'91208456',
       'contractId'=>'91231333',
    ];//广东手机直播套餐299元-7折24个月合约
    const UNICOM_THREE = [
       'goodsId'=>'512208096406',
       'productId'=>'91208457',
       'contractId'=>'91231333',
    ];//广东手机直播套餐399元-7折24个月合约
    //套餐
    const UNICOM_LIST = [
        '512208096403'=>'月租139元',
        '512208096405'=>'月租209元',
        '512208096406'=>'月租279元'
    ];
    
    //返回结果
    const RES_SUCCESS = 1;
    const RES_FAIL = 2;
    
    //获取套餐名称
    public static function getUnicomGoodsName($goodsId){
        $list = self::UNICOM_LIST;
        return isset($list[$goodsId]) ? $list[$goodsId] : '';
    }
    /**
     * 订单同步接口
     */
    static public function nuicomOrderSync($input){
        $chnlcode = "5114199492"; //发展人编码
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/cb_order/orderSync/b1/"; //线上
        $token = self::unicomLogin();
        $goodsId = isset($input['goodsId'])?$input['goodsId']:'512208096403'; //商品id
        $baseProductId = isset($input['baseProductId'])?$input['baseProductId']:'91208411';//基本产品id
        $contractId = isset($input['contractId'])?$input['contractId']:'91231333';//合约ID
        
        $postName = isset($input['postName'])?$input['postName']:'张瑞键'; //邮递姓名
        $postNumber = isset($input['postNumber'])?$input['postNumber']:'17605027690'; //联系电话
        $postProvinceCode = isset($input['postProvinceCode'])?$input['postProvinceCode']:'440000'; //收货地址省份编码（6位）
        $postCityCode = isset($input['postCityCode'])?$input['postCityCode']:'440100'; //收货地址地市编码（6位）
        $postDistrictCode = isset($input['postDistrictCode'])?$input['postDistrictCode']:'440106'; //收货地址区县编码（6位）
        $postAddr = isset($input['postAddr'])?$input['postAddr']:'中景大厦A座1217'; //收货地址详情
        
        $certId = isset($input['certId'])?$input['certId']:'35042619990714451X'; //号主身份证id
        $certName = isset($input['certName'])?$input['certName']:'张瑞键'; //号主姓名
        
        $order_no = isset($input['order_no'])?$input['order_no']:'123456'; //订单编号
        
        $phoneNum = isset($input['phoneNum'])?$input['phoneNum']:'17576024957'; //选占号码
        

        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token, //登录接口返回的token
            "provinceCode"=>"51",//号码归属省分编码：如 广东 51 (省市编码见附件)
            "cityCode"=>"510", //号码归属地市编码：如 广州市 510 (省市编码见附件)
            "body"=>array(
                "goodsInfo"=>array(  //商品信息
                    "goodsId"=>$goodsId,  //商品id,一般为51开头的12位编码，需与选号接口用的编码保持一致
                    "baseProductId"=>$baseProductId, //基本产品id
                    "firstMonthId"=>"A000011V000001 ", //首月资费方式id A000011V000001 （全月）A000011V000002（半月）A000011V000003（套外）A000011V000004（首月按天付费）
                    "firstMonthName"=>"全月计费" //首月资费方式
                ),
                "postInfo"=>array(  //邮递信息
                    "postName"=>$postName,  //邮递姓名
                    "postNumber"=>$postNumber,  //联系电话
                    "webProvince"=>$postProvinceCode,  //邮递省份编码（6位）
                    "webCity"=>$postCityCode,  //邮递地市编码（6位）
                    "webCounty"=>$postDistrictCode,  //邮递区县编码（6位）
                    "address"=>$postAddr //邮递地址
                ),
                "numberListInfo"=>array(  //选择号码信息
                    array(
                        "number"=>$phoneNum, //号码(号码预占接口已成功预占的号码)
                        "provinceCode"=>"51", //号码省份(两位地址编码)
                        "cityCode"=>"510", //号码地市(三位地址编码)
                        "mainFlag"=>"1", //主卡标记 1 主卡 0 副卡(默认传主卡)
                        "userFlag"=>"1", //新老用户标记 1新用户 2老用户(默认传新用户)
                        "certId"=>$certId, //号主身份证id
                        "certName"=>$certName, //号主身份证姓名
                        "isOpenCF"=>"0" //是否开通呼叫转移服务: 0不开通 1开通
                    )
                ),
                "feeInfo"=>array(
                    "orderTotalFee"=>"0" //订单总费用(单位：厘)，一般为0元订单，传0
                ),
                "contractInfo"=>array( //合约信息
                    "contractId"=>$contractId//合约ID
                ),
                "devCode"=>$chnlcode, //10位发展人编码
                "channel"=>"08-2278-anwa-9999", //17位触点编码
                "partnerOrderId"=>$order_no //合作方订单id
            )
        );
        //触点 08-2278-anwa-9999，08-2278-anw9-9999，08-2278-anw8-9999，08-2278-anwd-9999，08-2278-anwc-9999，08-2278-anwb-9999

        $jsonData = json_encode($params);
        
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        $result = json_decode($content,true);  //返回结果
        return $result;
    }
    /**
     * 号码预占接口
     */
    static public function unicomChange($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/num/state/change/v1/"; //线上
        $token = self::unicomLogin();
        $certNo = isset($input['certNo'])?$input['certNo']:'35042619990714451X'; //身份证号码
        $serialNumber = isset($input['serialNumber'])?$input['serialNumber']:'17576024957'; //待预占的号码
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "provinceCode"=>"51",//号码归属省分编码：如 广东 51 (省市编码见附件)
            "cityCode"=>"510", //号码归属地市编码：如 广州市 510 (省市编码见附件)
            "certNo"=>$certNo, //身份证号码
            "serialNumber"=>$serialNumber, //待预占的号码
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    /**
     * 风控校验接口-用于合作方校验客户身份证是否满足广州开户要求
     */
    static public function unicomCheck($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/certid_check_gz/v1/"; //线上
        $token = self::unicomLogin();
        $certName = isset($input['certName'])?$input['certName']:'张瑞键'; //入网人身份证姓名
        $certNo = isset($input['certNo'])?$input['certNo']:'35042619990714451X'; //入网人身份证号码（身份证中的X要求大写）
        $contactPhone = isset($input['postNumber'])?$input['postNumber']:'17605027690'; //联系电话
        
        $postProvinceCode = isset($input['postProvinceCode'])?$input['postProvinceCode']:'110110'; //收货地址省份编码（6位）
        $postCityCode = isset($input['postCityCode'])?$input['postCityCode']:'110110'; //收货地址地市编码（6位）
        $postDistrictCode = isset($input['postDistrictCode'])?$input['postDistrictCode']:'110110'; //收货地址区县编码（6位）
        $postAddr = isset($input['postAddr'])?$input['postAddr']:'中景大厦'; //收货地址详情
        
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "provinceCode"=>"51",//号码归属省分编码：如 广东 51 (省市编码见附件)
            "cityCode"=>"510", //号码归属地市编码：如 广州市 510 (省市编码见附件)
            "certId"=>$certNo, //入网人身份证号码（身份证中的X要求大写）
            "certName"=>$certName, //入网人身份证姓名
            "contactPhone"=>$contactPhone, //联系电话
            "postProvinceCode"=>$postProvinceCode,//收货地址省份编码（6位）
            "postCityCode"=>$postCityCode,//收货地址地市编码（6位）
            "postDistrictCode"=>$postDistrictCode,//收货地址区县编码（6位）
            "postAddr"=>$postAddr,//收货地址详情
            "channel"=>"08-2278-anwa-9999", //17位触点编码
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    
    /**
     * 选号
     */
    static public function unicomSelect($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/num/select/v1/"; //线上
        $token = self::unicomLogin();
        $goodsId = isset($input['goodsId'])?$input['goodsId']:'512208096403'; //商品id
        if(!empty($input['searchValue'])){
            $searchValue = $input['searchValue'];
            $searchType = "01";
        }else{
            $searchValue ="";
            $searchType = "";
        }
        
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "provinceCode"=>"51",//号码归属省分编码：如 广东 51 (省市编码见附件)
            "cityCode"=>"510", //号码归属地市编码：如 广州市 510 (省市编码见附件)
            "qryType"=>"02", //查询类型：一般传 02
            "searchCategory"=>"3", //查询类型：1-普通选号。2-靓号选号。3-全部（普通+靓号）
            "goodsId"=>$goodsId, //商品商城产品ID，全国产品为98开头，本地产品为51开头的编码，参照附件内容，需与订单提交接口保持一致
            "searchType"=>$searchType,   # 若不考虑匹配号码，默认传空值,传02 匹配最后四位，传01 匹配最后8位 (值可为空)
            "searchValue"=>$searchValue,  # 若需要匹配具体号码，则需要传值，searchType传02
            "amounts"=>"10"  //支持10或100，传空默认返回100
        );
        
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    
    /**
     * 收货地址省市区和号码省市信息查询
     */
    static public function unicomPostInfo($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/postInfo/qry/"; //线上
        $token = self::unicomLogin();
        $provinceCode = isset($input['provinceCode'])?$input['provinceCode']:'51'; //省份ecss编码
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "provinceCode"=>$provinceCode,//省份ecss编码
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    
    /**
     * 用户协议信息查询-用于合作方查询并展示商品入网协议信息，需用户同意勾选协议签订。
     */
    static public function unicomProtocal($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/protocal/query"; //线上
        $token = self::unicomLogin();
        $goodsId = isset($input['goodsId'])?$input['goodsId']:'512208096403'; //商品id
        $certId = isset($input['certId'])?$input['certId']:'440223198652365412'; //号主身份证id
        $certName = isset($input['certName'])?$input['certName']:'张野'; //号主姓名
        $phoneNum = isset($input['phoneNum'])?$input['phoneNum']:'15022232323'; //选占号码
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            'goodsId' => $goodsId,//登录接口返回的token
            "provinceCode"=>"51",//号码归属省分编码：如 广东 51 (省市编码见附件)
            "cityCode"=>"510", //号码归属地市编码：如 广州市 510 (省市编码见附件)
            "certId"=>$certId, //号主身份证id
            "certName"=>$certName, //号主身份证姓名
            "phoneNum"=>$phoneNum, //选占号码
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    
    /**
     * 京配地址查询-用于合作方渠道查询订单配送地址是否为京东物流覆盖范围
     */
    static public function unicomCheckAddress($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/check_jd_address/v1"; //线上
        $token = self::unicomLogin();
        
        $postProvinceCode = isset($input['postProvinceCode'])?$input['postProvinceCode']:'110110'; //收货地址省份编码（6位）
        $postCityCode = isset($input['postCityCode'])?$input['postCityCode']:'110110'; //收货地址地市编码（6位）
        $postDistrictCode = isset($input['postDistrictCode'])?$input['postDistrictCode']:'110110'; //收货地址区县编码（6位）
        $postAddr = isset($input['postAddr'])?$input['postAddr']:'中景大厦'; //收货地址详情
        
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "prov_code"=>$postProvinceCode,//收货地址省份编码（6位）
            "city_code"=>$postCityCode,//收货地址地市编码（6位）
            "eparchy_code"=>$postDistrictCode,//收货地址区县编码（6位）
            "address"=>$postAddr,//收货地址详情
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return json_decode($content,true);
    }
    
    /**
     * 京东物流轨迹查询-用于合作方渠道查询订单的物流轨迹
     */
    static public function unicomExpressTrace($input) {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/cucc_express_trace/v1"; //线上
        $token = self::unicomLogin();
        $order_id = isset($input['orderId'])?$input['orderId']:'2262120543962511'; //待查询的联通订单ID(20位)
        $params = array(
            'source' => $source, //联通分配的source
            'token' => $token,//登录接口返回的token
            "order_id"=>$order_id,//待查询的联通订单ID(20位)
        );
        $jsonData = json_encode($params);
        $content = self::buerCurl($url,$jsonData);
        // dd($content);
        return $content;
    }
    
    /**
     * 登录
     */
    static public function unicomLogin() {
        $source = "haohai";   //账号
        $passward = "w0eyozso"; //密码
        $url = "https://www.gzuni.com/apps/kingcard/api/v1/login/"; //线上
        $params = array(
            'source' => $source,
            'password' => $passward
        );
        $jsonData = json_encode($params);
        // dd($jsonData);
        $content = self::buerCurl($url,$jsonData);
        $content = json_decode($content,true);
        // dd($content);
        return $content['data'];
    }
    
    /**
     * 请求接口返回内容
     * @param  string $url [请求的URL地址]
     * @param  string $data [请求的参数]
     */
    static private function buerCurl($url,$data=null){
        $curl = curl_init();
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($curl,CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl,CURLOPT_POST,1);
        curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
        curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
        
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            "Content-Type: application/json"
        ));
        $output = curl_exec($curl);
        // dd($output);
        curl_close($curl);
        return $output;
    }

    
    

}
